from sqlalchemy import exists, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload

from src.infrastructure.db.models import User


async def get_users(session: AsyncSession, registered: bool = None) -> list[User]:
    q = select(User)
    if registered is not None:
        if registered:
            q = q.where(User.is_registered == 1)
        else:
            q = q.where(User.is_registered == 0)

    res = await session.execute(q)

    return res.scalars().all()


async def change_user_language(session: AsyncSession, user_id: int, lang_code: str) -> None:
    q = update(User).where(User.telegram_id == user_id).values(lang_code=lang_code)
    await session.execute(q)

    await session.commit()
